package com.sunwayhorizo.health.business.registries.organization.query;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.Organization;
import com.sunwayhorizo.health.hl7.rim.Role;
import com.sunwayhorizo.health.hl7.vocabulary.EntityCode;
import com.sunwayhorizo.health.hl7.vocabulary.RoleCode;

@Name("OrganizationQuery")
@Stateless
public class OrganizationQuery implements OrganizationQueryLocal
{

    @PersistenceContext(unitName = "sunwayhealth")
    private EntityManager em;

    
    @SuppressWarnings("unchecked")
    public Role findRoleByEntityId(String entyId) throws Hl7Exception
    {
        if (StrUtil.isEmpty(entyId))
        {
            return null;
        }
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select r from Role r ");
        jpql.append(" where  r.codeEtsId='" + RoleCode.HOSPITAL.toString() + "' ");
        jpql.append(" and r.playerEntities.entityId="+entyId +" ");
        List<Role> list = em.createQuery(jpql.toString()).getResultList();
        if (ListUtil.isNotEmpty(list))
        {
            return list.get(0);
        }
        return null;
    }
    
    
    @SuppressWarnings("unchecked")
    public List<Role> findOrgRoleList(String orgName,String orgCode) throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select r from Role r ");
        jpql.append(" left join r.playerEntities enty ");
        String cond = " where  r.codeEtsId='" + RoleCode.HOSPITAL.toString() + "' ";
        if(StrUtil.isNotEmpty(orgName))
        {
            jpql.append(" left join enty.entyNamesList en ");
            cond += " and en.formatted like '%"+orgName+"%'";
        }
        if(StrUtil.isNotEmpty(orgCode))
        {
            jpql.append(" left join enty.entyIiList ii ");
            cond += " and ii.extensionTxt='"+orgCode+"'";
        }
        cond += " order by r.creationDate asc";
        List<Role> list = em.createQuery(jpql.toString()+cond).getResultList();
        return list;
    }
    
    @SuppressWarnings("unchecked")
    public Organization findOrganizationByOrgName(String orgName) throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select o from Organization o ");
        String cond = "where o.codeEtsId='" + EntityCode.HOSPITAL.toString() + "'";
        if(StrUtil.isNotEmpty(orgName))
        {
            jpql.append(" left join o.entyNamesList en ");
            cond += " and en.formatted='"+orgName+"'";
        }
        cond +=" order by o.creationDate asc";
        List<Organization> list = em.createQuery(jpql.toString()+cond).getResultList();
        return (list.isEmpty()? null: list.get(0));
    }
    
    @SuppressWarnings("unchecked")
    public List<Organization> findOrganizationList() throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select o from Organization o ");
        jpql.append(" where o.codeEtsId='" + EntityCode.HOSPITAL.toString() + "' ");
        jpql.append(" order by o.creationDate asc");
        List<Organization> list = em.createQuery(jpql.toString()).getResultList();
        return list;
    }
    
    @SuppressWarnings("unchecked")
    public Role findOrgRoleByCode(String code) throws Hl7Exception
    {
        StringBuilder jpql = new StringBuilder();
        jpql.append(" select r from Role r ");
        jpql.append(" left join r.playerEntities enty ");
        jpql.append(" left join enty.entyIiList ii ");
        jpql.append(" where  r.codeEtsId='" + RoleCode.HOSPITAL.toString() + "' ");
        jpql.append(" and ii.extensionTxt='"+code+"'");
        List<Role> list = em.createQuery(jpql.toString()).getResultList();
        if (ListUtil.isNotEmpty(list))
        {
            return (Role) list.get(0);
        }
        return null;
    }

    
    @SuppressWarnings({ "rawtypes"})
    public boolean findDepartByOrgIds(List orgIds) throws Hl7Exception
    {
        boolean b = false;
        if (ListUtil.isNotEmpty(orgIds))
        {
            StringBuilder jpql = new StringBuilder(" ");
            jpql.append(" select r.roleId from Role r ");
            jpql.append(" where  r.codeEtsId='" + EntityCode.DEPARTMENT.toString()+ "' ");
            jpql.append(" and (");
            for (int i = 0; i < orgIds.size(); i++)
            {
                String id = (String) orgIds.get(i);
                if (i < orgIds.size() - 1)
                {
                    jpql.append(" r.scoperEntities.entityId=" + id + " or ");
                }
                else
                {
                    jpql.append(" r.scoperEntities.entityId=" + id);
                }
            }
            jpql.append(" ) ");
            List list = em.createQuery(jpql.toString()).getResultList();
            if (ListUtil.isNotEmpty(list))
            {
                b = true;
            }
        }
        return b;
    }
    
}
